/**
 * 响应也可以分为深响应和浅响应
 */
let activeEffect
const bucket = new WeakMap()
let effectStack = []
const ITERATE_KEY = Symbol()
function track(target, key) {
  let depsMap = bucket.get(target)
  if (!depsMap) {
    bucket.set(target, (depsMap = new Map()))
  }
  let deps = depsMap.get(key)
  if (!deps) {
    depsMap.set(key, (deps = new Set()))
  }
  if (activeEffect) {
    deps.add(activeEffect)
    activeEffect.deps.push(deps)
  }
}

function trigger(target, key, type) {
  const depsMap = bucket.get(target)
  if (!depsMap) return
  const effects = depsMap.get(key)
  const effectsToRun = new Set()
  effects &&
    effects.forEach((effect) => {
      if (effect !== activeEffect) {
        effectsToRun.add(effect)
      }
    })

  if (type === 'ADD' || type === 'DELETE') {
    const iterateEffects = depsMap.get(ITERATE_KEY)

    iterateEffects &&
      iterateEffects.forEach((effect) => {
        if (effect !== activeEffect) {
          effectsToRun.add(effect)
        }
      })
  }

  effectsToRun.forEach((effect) => {
    if (effect.options.schedule) {
      effect.options.schedule(effect)
    } else {
      effect()
    }
  })
}
// 封装通用的响应式函数
// isShallow表示是否浅响应
function createReactive(data, isShallow = false) {
  const obj = new Proxy(data, {
    get(target, key, receiver) {
      if (key === 'raw') {
        return target
      }
      // 获取对应的值
      const res = Reflect.get(target, key, receiver)
      track(target, key)

      // 如果是浅响应, 直接返回
      if (isShallow) {
        return res
      }

      // 如果返回一个对象, 递归
      if (typeof res === 'object' && res !== null) {
        return createReactive(res)
      }

      return res
    },
    set(target, key, value, receiver) {
      const oldValue = target[key]
      const type = Object.prototype.hasOwnProperty.call(target, key) ? 'SET' : 'ADD'
      const res = Reflect.set(target, key, value, receiver)
      if (receiver.raw === target) {
        if (oldValue !== value && (oldValue === oldValue || value === value)) {
          trigger(target, key, type)
        }
      }
      return res
    },
    has(target, key) {
      return Reflect.has(target, key)
    },
    ownKeys(target) {
      track(target, ITERATE_KEY)
      return Reflect.ownKeys(target)
    },
    deleteProperty(target, key) {
      const hadKey = Object.prototype.toString.call(target, key)
      const res = Reflect.deleteProperty(target, key)
      if (hadKey && res) {
        trigger(target, key, 'DELETE')
      }
      return res
    }
  })
  return obj
}
function cleanup(effectFn) {
  for (let i = 0; i < effectFn.deps.length; i++) {
    const deps = effectFn.deps[i]
    deps.delete(effectFn)
  }
  effectFn.deps.length = 0
}

function effect(fn, options = {}) {
  const effectFn = () => {
    cleanup(effectFn)
    activeEffect = effectFn
    effectStack.push(effectFn)
    const res = fn()
    effectStack.pop()
    activeEffect = effectStack[effectStack.length - 1]
    return res
  }
  effectFn.deps = []
  effectFn.options = options
  if (!options.lazy) {
    effectFn()
  }
  return effectFn
}

// 浅响应函数
function shallowReactive(obj) {
  return createReactive(obj, true)
}

// 深响应函数
function reactive(obj) {
  return createReactive(obj)
}

// 浅响应测试
const data1 = { bar: { foo: '1' } }
const obj1 = shallowReactive(data1)

effect(() => {
  console.log(obj1.bar.foo, 'shallow effect')
})

// 深响应测试
const data2 = { bar: { foo: '1' } }
const obj2 = reactive(data2)

effect(() => {
  console.log(obj2.bar.foo, 'deep effect')
})

obj1.bar.foo = '12'
/**
 * 1 shallow effect
 */

obj2.bar.foo = '12'
/**
 * 1 deep effect
12 deep effect
 */
/**
 * 这样一来, shallowReactive只让对象的第一层属性变成了响应式
 * reactive由于存在递归, 所以, 所有子属性都是响应式了
 */
